package com.zhxi.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhxi.common.utils.PageUtils;
import com.zhxi.common.utils.Query;
import com.zhxi.common.utils.R;
import com.zhxi.gulimall.product.dao.BrandDao;
import com.zhxi.gulimall.product.dao.CategoryBrandRelationDao;
import com.zhxi.gulimall.product.dao.CategoryDao;
import com.zhxi.gulimall.product.entity.BrandEntity;
import com.zhxi.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zhxi.gulimall.product.service.BrandService;
import com.zhxi.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联
 *
 * @Author: zhxi
 * @Email: 664480310@qq.com
 * @Date: 2024-08-19 17:41:15
 * @Version: 1.0.0
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    BrandService brandService;
    @Autowired
    CategoryBrandRelationDao relationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public R catelogList(Long id) {
        List<CategoryBrandRelationEntity> list = list(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId, id));
        return R.ok().put("data", list);
    }

    @Override
    public R saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        String brandName = brandDao.selectById(categoryBrandRelation.getBrandId()).getName();
        String cateName = categoryDao.selectById(categoryBrandRelation.getCatelogId()).getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(cateName);
        save(categoryBrandRelation);
        return R.ok();
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        lambdaUpdate().eq(CategoryBrandRelationEntity::getBrandId, brandId).set(CategoryBrandRelationEntity::getBrandName, name).update();
    }

    @Override
    public void updateCategory(Long catId, String name) {
        lambdaUpdate().eq(CategoryBrandRelationEntity::getCatelogId, catId).set(CategoryBrandRelationEntity::getBrandName, name).update();

    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

}